package com.firmboy.io_collection.io;

import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.file.*;
import java.util.List;

/**
 * NIO测试类
 *
 * @author firmboy
 * @create 2018-05-01 下午10:23
 **/
public class NIOTest {

    @Test
    public void nioTest() throws IOException {
        //NIO对文件的操作
        FileChannel channel = new FileOutputStream("/firmboy/workspace/test/test.txt").getChannel();
        channel.write(ByteBuffer.wrap("hello world2".getBytes()));

        channel.close();


    }


    @Test
    public void pathTest() throws IOException {
        //Path对象的操作
        Path path = Paths.get("/firmboy/workspace/test/");
        WatchService watchService = FileSystems.getDefault().newWatchService();

        WatchKey register = path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);

        while(true){
            List<WatchEvent<?>> watchEvents =
                    register.pollEvents();

            for (WatchEvent event:watchEvents
                 ) {
                System.out.println(event.kind().name());
            }
        }


    }

    @Test
    public void socketTest(){



    }

}
